<template>
    <div>
        <el-dialog
            :title="$t('agent.agentEdit.title')"
            :visible="visibleEdit"
            class="el-dialog-lg"
            @close="()=>{$store.commit('setVisibleEdit',false)}">
            <el-form ref="form" :model="form" label-width="200px" >
                <el-row :gutter="24">
                    <el-col :span="11">
                        <el-form-item :label="$t('agent.firmName')" prop="firmName" :rules="[{ required: true, message: $t('index_js_notNull')},{validator:test_firmName,trigger:'blur'}]">
                            <el-input v-model="form.firmName" placeholder=""></el-input>
                        </el-form-item>
                        <el-form-item :label="$t('agent.firmShort')">
                            <el-input v-model="form.firmShort" placeholder=""></el-input>
                        </el-form-item>
                        <el-form-item :label="$t('country')" prop="country" :rules="[{ required: true, message: $t('index_js_notNull')}]" >
                            <el-select v-model="form.country" filterable :placeholder="$t('pleaseSelect')">
                                <el-option
                                v-for="item in selectCountryData"
                                :key="item.enName"
                                :label="item.enName"
                                :value="item.enName">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item :label="$t('address')">
                            <el-input v-model="form.address" placeholder=""></el-input>
                        </el-form-item>
                        <el-form-item :label="$t('email')" prop="email" :rules="[{ required: true, message: $t('index_js_notNull')},{validator:test_email,trigger:'blur',}]">
                            <el-input v-model="form.email" placeholder=""></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="11">
                        <el-form-item :label="$t('contact')">
                            <el-input v-model="form.relation" placeholder=""></el-input>
                        </el-form-item>
                        <el-form-item :label="$t('telphone')" prop="telphone" :rules="[{validator:test_telphone,trigger:'blur'}]">
                            <el-input v-model="form.telphone" placeholder=""></el-input>
                        </el-form-item>
                        <el-form-item :label="$t('introduce')">
                            <el-input v-model="form.instroduce" type="textarea" :rows="2" placeholder=""></el-input>
                        </el-form-item>
                        <el-form-item :label="$t('remark')">
                            <el-input v-model="form.remark" type="textarea" :rows="2" placeholder=""></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="()=>{$store.commit('setVisibleEdit',false)}">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="save('form')">{{$t('save')}}</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import md5 from 'js-md5'
export default {
    props: {
        selectCountryData:{
            type:Array,
            default:() => {return []}
        },
        rowData:{
            type:Object,
            default:() => {return {}}
        }
    },
    data() {
        return {
            form:{
                type:'',
                firmName:'',
                firmShort:'',
                country:'',
                relation:'',
                telphone:'',
                email:'',
                address:'',
                instroduce:'',
                remark:''
            },
            test_firmName:{},
            test_telphone:{},
            test_email:{}
        };
    },
    computed: {
        visibleEdit(){
            return this.$store.state.sysStore.visibleEdit
        }
    },
    watch:{
        rowData:{
            async handler(nv,ov){
                // let id=nv.sid||ov.sid
                // if(id){
                //     let res = await this.API.getAgentDataById({
                //         id:id
                //     })
                //     for(let key in this.form){
                //         if(key=='type') this.form.type=res[key].toString()
                //         else this.form[key]=res[key];
                //     }
                //     this.form.id=id
                // }
                    for(let key in this.form){
                        if(key=='type') this.form.type=nv[key].toString()
                        else this.form[key]=nv[key] == null ? '':nv[key]
                    }
                    this.form.id=nv.sid
            },
            deep:true
        }
    },
    mounted(){
        this.test_firmName = async(rule, value, callback) => {
            let res = await this.API.agentFirmisUnique({
                firmName:this.form.firmName,
                type:this.form.type,
                notId:this.form.id
            })
            if(!res.success){
                if(this.form.type==3){
                    callback(new Error(this.$t('agent.msg_unique_firmName')));
                }else{
                    callback(new Error(this.$t('install.msg_unique_installName')));
                }
            }else{
                callback()
            }
        }
        this.test_telphone = (rule, value, callback) => {
            if(value!=''&&!this.Regs.phone_reg.test(value)){
                callback(new Error(this.$t('login.js_sign_phone_error')));
            }else{
                callback()
            }
        }
        this.test_email = (rule, value, callback) => {
            if(value!=''&&!this.Regs.email_reg.test(value)){
                callback(new Error(this.$t('login.js_sign_email_error')));
            }else{
                callback()
            }
        }
    },
    methods: {
        save(formName){
            this.$refs[formName].validate((valid) => {
              if (valid) {
                this.commitForm()
              } else {
                console.log('error submit!!');
                return false;
              }
            });
        },
        async commitForm(){
            let res = await this.API.agentEdit(Object.assign({},this.form))
            console.log(res);
            if(res.success){
                this.$message.success(this.$t('index.control_success'));
            }else{
                this.$message.error(this.$t('index.control_fail'));
            }
            this.$store.commit('setVisibleEdit',false)
            this.$emit('uploadPage',true)
        }
    }
};
</script>

<style  lang="less">
</style>
